<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <!-- ArcGIS API for JavaScript本地部署地址 -->
    <!-- <link rel="stylesheet" hrel="http://localhost/arcgis_js_v416_sdk/arcgis_js_api/library/4.16/esri/css/main.css" />
    <script src="http://localhost/arcgis_js_v416_sdk/arcgis_js_api/library/4.16/dojo/dojo.js"></script> -->

    <!-- jquery-2.0.3.min.js是jquery-2.0.3.js的编译版本 -->
    <!-- jQuery 库是一个 JavaScript 文件,下面在线引用它。 -->
    <!-- <script src="https://code.jquery.com/jquery-2.0.3.min.js"></script> -->

    <link rel="stylesheet" href="https://js.arcgis.com/4.16/esri/themes/light/main.css" />
    <script src="https://js.arcgis.com/4.16/"></script>

    <style>
        html,
        body,
        #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
        }
    </style>
    <script>
        require([
            "esri/Map",
            "esri/geometry/Geometry",
            "esri/views/MapView",
            "esri/layers/FeatureLayer",
            "esri/layers/MapImageLayer",
            "esri/geometry/Point",
            "esri/geometry/Polyline",
            "esri/geometry/Polygon",
            "esri/views/MapView",
            "esri/Graphic",
            "esri/Color",
            "esri/tasks/support/BufferParameters",
            //"esri/tasks/support/GeometryService",
            "esri/symbols/SimpleMarkerSymbol",
            "esri/symbols/SimpleFillSymbol",
            "esri/symbols/SimpleLineSymbol",
            "esri/geometry/SpatialReference",
            "esri/tasks/QueryTask",
            "esri/tasks/support/Query",
            "dojo/domReady!"
        ], function(
            Map,
            Geometry,
            MapView,
            FeatureLayer,
            MapImageLayer,
            Point,
            Polyline,
            Polygon,
            MapView,
            Graphic,
            Color,
            BufferParameters,
            //GeometryService,
            SimpleMarkerSymbol,
            SimpleFillSymbol,
            SimpleLineSymbol,
            SpatialReference,
            QueryTask,
            Query) {
            //创建一个地图Map实例
            var map = new Map({
                basemap: "streets"
                    //属性：allLayers、ground、layers
            });
            //创建一个MapView实例（用于2D查看）并引用该地图实例
            //为了使地图在DOM中对用户可见，必须创建一个MapView并至少引用两个对象：一个Map实例和一个DOM元素。分别在map和container属性中设置。
            var view = new MapView({
                container: "viewDiv",
                map: map,
                zoom: 10,
                //center: [55, 65] // longitude, latitude
                //center: [119.38923, 25.679553]
                //center: [125.268891, 36.029324]
                center: [113.199833, 33.775258] //祥云公园
            });

            // var layer = new MapImageLayer("http://localhost:6080/arcgis/rest/services/qingdao/MapServer");
            // map.add(layer);

            const featurelayer = new FeatureLayer({
                url: "http://localhost:6080/arcgis/rest/services/xygy/MapServer/0"
                    // popupTemplate: template
                    //https://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer/0
            });
            map.add(featurelayer);

            view.on("click", function(event) {
                view.hitTest(event).then(function(response) {
                    console.log(response.results);
                    console.log(response.results[0]);
                    console.log(response.results[0].graphic);
                    console.log(response.results[0].mapPoint.x);
                    console.log(response.results[0].graphic.attributes.OBJECTID);
                    console.log(response.results[0].graphic.layer);
                    var layerindex = response.results[0].graphic.layer;
                    var uid = response.results[0].graphic.attributes.OBJECTID;
                    var gr = response.results[0].graphic.geometry;
                    queryinfo(uid, gr);


                    function queryinfo(uid, gr) {
                        var querytask = new QueryTask({
                            url: "http://localhost:6080/arcgis/rest/services/xygy/MapServer/0"
                        });
                        var query = new Query();
                        //query.where = "Id=\'" + uid + "\'";
                        query.where = "name='椿树'";
                        //query.where = "Id='0'";
                        query.outFields = ["*"];
                        query.returnGeometry = true;
                        querytask.execute(query).then(function(results) {
                            const template = {
                                title: "Trail name: {trailName}",
                                content: "difficultyLevel"
                                    // fieldInfos: [{
                                    //     fieldName: "trailName"
                                    // }, {
                                    //     fieldName: "difficulty"
                                    // }],
                                    // actions: [editThisAction]
                            };
                            var symbol = new SimpleFillSymbol();
                            var graphic = new Graphic({
                                geometry: gr,
                                symbol: symbol,
                                popupTemplate: popupTemplate1
                            });
                        }, function(error) {
                            console.log(error);
                        });
                    };
                });
            });
        });
    </script>
</head>

<body>
    <div id="viewDiv"></div>
</body>

</html>

// if (response.results.length) { // var graphic = response.results.filter(function(result) { // // check if the graphic belongs to the layer of interest // return result.graphic.layer === graphicsLayer; // })[0].graphic; // console.log(graphic.geometry);
// }